package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import sun.awt.image.GifImageDecoder;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {

    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 提交订单
     * @param orders
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void submit(Orders orders) {
        Long currentId = BaseContext.getCurrentId();
        User user = userService.getById(currentId);

        // 1 查询购物车
        List<ShoppingCart> shoppingCartList = shoppingCartService.shoppingList();
        if(CollectionUtils.isEmpty(shoppingCartList)) {
            throw new CustomException("购物车为空，不能下单");
        }

        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressBookService.getById(addressBookId);
        if(addressBook == null) {
            throw new CustomException("用户地址信息有误，不能下单！");
        }

        BigDecimal orderAoount = new BigDecimal("0");
        for (ShoppingCart shoppingCart : shoppingCartList) {
            BigDecimal amount = shoppingCart.getAmount();
            Integer number = shoppingCart.getNumber();
            BigDecimal dishAmount = amount.multiply(BigDecimal.valueOf(number));
            orderAoount = orderAoount.add(dishAmount);
        }

        // 新增订单
        orders.setNumber(IdWorker.getIdStr());
        orders.setUserId(currentId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        // 金额
        orders.setAmount(orderAoount);
        orders.setPhone(user.getPhone());
        String address = addressBook.getProvinceName() + addressBook.getDistrictName() + addressBook.getCityName() + addressBook.getDetail();
        orders.setAddress(address);
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());
        save(orders);

        // 新增订单明细
        List<OrderDetail> orderDetailList = shoppingCartList.stream().map(shoppingCart -> {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orders.getId());
            BeanUtils.copyProperties(shoppingCart, orderDetail);
            return orderDetail;
        }).collect(Collectors.toList());
        orderDetailService.saveBatch(orderDetailList);

        // 清空购物车
        shoppingCartService.clear();
    }
}
